// src/components/profile/Profile.jsx
import React from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { useAuth } from '../../contexts/AuthContext';
import TrackData from './TrackData';
import OrderHistory from './OrderHistory';

const Profile = () => {
  const { user, logout } = useAuth();
  const navigate = useNavigate();

  if (!user) {
    navigate('/login');
    return null;
  }

  const handleLogout = () => {
    logout();
    navigate('/login');
  };

  return (
    <div className="space-y-6">
      <div className="bg-white rounded-lg shadow-md p-6">
        <div className="flex items-center space-x-4">
          <div className="w-20 h-20 bg-blue-100 rounded-full flex items-center justify-center">
            <span className="text-2xl text-blue-600">
              {user.username[0].toUpperCase()}
            </span>
          </div>
          <div className="flex-1">
            <h1 className="text-2xl font-bold text-gray-900">{user.username}</h1>
            <p className="text-gray-600">加入时间：{new Date(user.createdAt).toLocaleDateString()}</p>
          </div>
          <button
            onClick={handleLogout}
            className="bg-red-600 text-white px-4 py-2 rounded-md hover:bg-red-700"
          >
            退出登录
          </button>
        </div>
      </div>

      <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
        <div className="bg-white rounded-lg shadow-md p-6">
          <h2 className="text-xl font-semibold mb-4">快速操作</h2>
          <div className="grid grid-cols-2 gap-4">
            <Link
              to="/"
              className="flex flex-col items-center p-4 bg-gray-50 rounded-lg hover:bg-gray-100"
            >
              <span className="text-2xl mb-2">🏎️</span>
              <span>租赁赛车</span>
            </Link>
            <Link
              to="/challenges"
              className="flex flex-col items-center p-4 bg-gray-50 rounded-lg hover:bg-gray-100"
            >
              <span className="text-2xl mb-2">🏆</span>
              <span>参加挑战</span>
            </Link>
            <Link
              to="/calendar"
              className="flex flex-col items-center p-4 bg-gray-50 rounded-lg hover:bg-gray-100"
            >
              <span className="text-2xl mb-2">📅</span>
              <span>查看日程</span>
            </Link>
            <Link
              to="/forum"
              className="flex flex-col items-center p-4 bg-gray-50 rounded-lg hover:bg-gray-100"
            >
              <span className="text-2xl mb-2">💬</span>
              <span>论坛讨论</span>
            </Link>
          </div>
        </div>

        <TrackData />
      </div>

      <OrderHistory />
    </div>
  );
};

export default Profile;